﻿#include"load.h"
#include<malloc.h>
#include<string.h>
Qvm_CodeCtl* QLoadCodeFile(char* path)
{
	Qvm_CodeCtl* re = malloc(sizeof(Qvm_CodeCtl));
	if (re == NULL)
		return NULL;
	re->fp = "";
	//strcpy(re->inf_fname, path);
	re->cur_index = 0;
	re->codes.c_list = (Qvm_Code*)malloc(sizeof(Qvm_Code) * 1024);
	re->codes.size = 0;
	re->fp = fopen(path, "rb");//注意，这里open后将不会关闭，知道close函数调用
	re->codes.max_size = 1024;
	if (re->fp == NULL)
	{
		free(re);
		return NULL;
	}
	QLoadReadNext(re);
	return re;
}
bool QLoadResetFile(char* path, Qvm_CodeCtl* ctl, bool isreadall)
{

}
bool QLoadReadNext(Qvm_CodeCtl* ctl)
{
	if (ctl == NULL)
		return false;
	if (ctl->codes.max_size - ctl->codes.size <= 1024)
	{
		Qvm_Code* n_code = (Qvm_Code*)malloc(sizeof(Qvm_Code) * ctl->codes.max_size + 1024);
		for (int i = 0; i < ctl->codes.max_size; i++)
		{
			n_code[i] = ctl->codes.c_list[i];
		}
		free(ctl->codes.c_list);
		ctl->codes.c_list = n_code;
		ctl->codes.max_size += 1024;
	}

	for (int i = 0; i < 1024; i++)
	{
		fread(&ctl->codes.c_list[ctl->codes.size], sizeof(Qvm_Code), 1, ctl->fp);
		if (feof(ctl->fp))
			break;
		ctl->codes.size++;
	}
	return true;
}
void QLoadCloseFile(Qvm_CodeCtl* ctl)
{

}
